<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class AuthAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->guard('admin')->guest()) {

            if(isset(auth()->guard('admin')->user()->id)){
                if(session()->getId() !=Redis::get(auth()->guard('admin')->user()->id)){
                    abort(403, '当前账户已在其他地方登录');
                }
            }



            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest( url('admin/login'));
            }
        }

        return $next($request);
    }
}
